<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="${pageContext.request.contextPath}/include/pageTitleBread.jsp"/>
<div id="page-content">
    <div class="col-lg-12">
        <div class="panel">
            <div class="panel-heading">
                <h3 class="panel-title">${pageInfo.innerTitle}</h3>
            </div>
            <form id="sysUserForm" enctype="multipart/form-data" method="post"
                  class="panel-body form-horizontal form-padding">
                <input type="hidden" name="flId" value="${sysUser.flId}">
                <div class="form-group">
                    <label class="col-md-3 control-label">关联角色</label>
                    <div class="col-md-5">
                        <select name="flRoleId" id="flRoleId" class="form-control">
                            <option>--请选择--</option>
                        </select>
                        <input type="hidden" id="role" value="${sysUser.flRoleId}">
                        <small class="help-block">请选择关联角色</small>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">用户登录名</label>
                    <div class="col-md-9">
                        <input type="text" name="flLoginName" class="form-control"
                               value="${sysUser.flLoginName == null ? '':sysUser.flLoginName}">
                        <small class="help-block">请输入用户名(用来登录系统)</small>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">用户头像</label>
                    <div class="col-md-9">
                        <input type="file" id="file" name="file">
                        <input type="hidden" id="flAvatar" name="flAvatar"
                               value="${sysUser.flAvatar == null ? '':sysUser.flAvatar}">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">用户姓名</label>
                    <div class="col-md-9">
                        <input type="text" name="flName" class="form-control"
                               value="${sysUser.flName == null ? '':sysUser.flName}">
                        <small class="help-block">请输入用户姓名</small>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">性别</label>
                    <div class="col-md-9">
                        <input id="flSex1" class="magic-radio" type="radio" name="flSex"
                               value="1" ${sysUser.flSex == 1 || sysUser.flSex == null? 'checked':''}>
                        <label for="flSex1">男</label>
                        <input id="flSex0" class="magic-radio" type="radio" name="flSex"
                               value="0" ${sysUser.flSex == 0 ? 'checked':''}>
                        <label for="flSex0">女</label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">电话</label>
                    <div class="col-md-9">
                        <input type="text" name="flPhone" class="form-control"
                               value="${sysUser.flPhone == null ? '':sysUser.flPhone}">
                        <small class="help-block">请输入电话</small>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">邮箱地址</label>
                    <div class="col-md-9">
                        <input type="text" name="flEmail" class="form-control"
                               value="${sysUser.flEmail == null ? '':sysUser.flEmail}">
                        <small class="help-block">请输入邮箱地址</small>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">用户状态</label>
                    <div class="col-md-9">
                        <input id="flStatusNormal" class="magic-radio" type="radio" name="flStatus"
                               value="0" ${sysUser.flStatus == 0|| sysUser.flStatus == null ? 'checked':''}>
                        <label for="flStatusNormal">正常</label>
                        <input id="flStatusEx" class="magic-radio" type="radio" name="flStatus"
                               value="1" ${sysUser.flStatus == 1 ? 'checked':''}>
                        <label for="flStatusEx">锁定</label>
                    </div>
                </div>
                <div class="panel-footer">
                    <div class="row">
                        <div class="col-sm-9 col-sm-offset-3">
                            <button class="btn btn-warning" type="reset">重置</button>
                            <button class="btn btn-mint" type="submit">保存</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var validPassword = {};//密码1
    var validPassword2 = {};//密码2
    var validUsername = {};//用户名 唯一性校验
    validUsername = {
        notEmpty: {
            message: '*用户名不能为空'
        },
        stringLength: {
            max: 15,
            message: '*用户名长度必须小于15'
        }
    };
    validUsername = {
        notEmpty: {
            message: '*用户名不能为空'
        },
        stringLength: {
            max: 15,
            message: '*用户名长度必须小于15'
        }
//            remote: {
//                message: '*用户名已被注册',
//                url: ctx + '/admin/user/validUsername',
//                delay: 1000
//            }
    };

    function save() {
        var data = common_ajax.ajaxFunc("/sysUser/saveOrUpdate", $('#sysUserForm').serialize(), "json", null);
        if (data.code === 0) {
            pointLion.alertMsg("保存成功!", "success", "small", function () {
                doPjax(ctx + '/sysUser');//跳转到列表页
            });
        } else {
            pointLion.alertMsg("操作失败!", "danger", "small", function () {
                doPjax(ctx + '/sysUser');//跳转到列表页
            });
        }
    }

    $(document).ready(function () {
        common_ajax.initFileUpload("file", "/commons/upload", "flAvatar",1);
        $.get("${pageContext.request.contextPath}/sysRole/initSelect", function (rst) {
            rst = JSON.parse(rst);
            var roleSelect = $("#flRoleId");
            var roleValue = $("#role").val();
            var selectHtml = "";
            $.each(rst, function (index, obj) {
                if (roleValue === obj.flId) {
                    selectHtml += "<option value='" + obj.flId + "' selected>" + obj.flName + "</option>";
                } else {
                    selectHtml += "<option value='" + obj.flId + "'>" + obj.flName + "</option>";
                }

            });
            roleSelect.html(selectHtml);
        });
        $('#sysUserForm').bootstrapValidator({
            excluded: [":hidden"],//关键配置，表示只对于隐藏域不进行验证，其他的表单元素都要验证
            fields: {
                "flRoleId": {
                    validators: {
                        notEmpty: {
                            message: '*角色不能为空!'
                        }
                    }
                },
                "flLoginName": {
                    validators: validUsername
                },
                "flLoginPwd": {
                    validators: validPassword
                },
                "password2": {
                    validators: validPassword2
                },
                "flName": {
                    validators: {
                        notEmpty: {
                            message: '*用户姓名不能为空'
                        },
                        stringLength: {
                            max: 15,
                            message: '*确认密码长度必须小于15'
                        }
                    }
                },
                "flPhone": {
                    validators: {
                        regexp: {
                            regexp: /^1(3|4|5|7|8)\d{9}$/,
                            message: '*移动电话格式不对'
                        }
                    }
                },
                "flEmail": {
                    validators: {
                        regexp: {
                            regexp: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/,
                            message: '*邮箱格式不对'
                        },
                        stringLength: {
                            max: 100,
                            message: '*您的邮箱地址也太长了吧'
                        }
                    }
                }
            }
        }).on("success.form.bv", function (e) {
            save();
            return false;//阻止表单跳转
        });
    });
</script>
